﻿#include "../../ctlButton.h"
#include <cmath>

namespace swui
{	
	void ctlButton::_position_event_(ctlPositionEvent* pe) 
	{
		if(pe->eventType == ctlPositionEventType::size)
			_calc_text_rectangle_(false);
		ctlRoot::_position_event_(pe);
	}
	void ctlButton::_mouse_event_(ctlMouseEvent* me)
	{
		if(me->eventModel != ctlMouseEventModel::cut) return;
		if(isSelectedEnabled() && me->eventType == ctlMouseEventType::leftPress)
			_button_selected = !_button_selected;
		IDrag::_mouse_event_(me);
		ISmoothTransform::_smooth_transform_mouse_event(me);
	}
	bool ctlButton::create(ctlRoot* parent, ctlRect rect, const ctlText& lpszText, const ctlStyleBase& style)
	{
		if(parent == 0 || isCreated() || !parent->isCreated()) return false;
		std::lock_guard<decltype(_eventTrans)> gurad(
			_eventTrans.getControlEventTrans(parent));
		_control_virtual_class_end = this;
		_basic_create_(rect, parent);
		_controlType = ctlControlType::ctlButton;
		_virtual_font_create();
		lockDrawing();
		_button_as_static = false;
		_button_selectedable = false;
		_button_selected = false;
		//设置调整模式
		if(rect.cx == 0 && rect.cy == 0) 
			setTextJudgeMode(ctlDirection::xy);
		else if(rect.cx == 0) setTextJudgeMode(ctlDirection::x);
		else if(rect.cy == 0) setTextJudgeMode(ctlDirection::y);
		else setTextJudgeMode(ctlDirection::none);
		setText(lpszText);
		//设置样式
		setStyle(style);
		setSelectedStyle(style);
		unlockDrawing();
		redraw();
		return true;
	}
	ctlDirection ctlButton::getTextJudgeMode() {
		return _judgeMode;
	}
	void ctlButton::setTextJudgeMode(ctlDirection mode) {
		_judgeMode = mode;
		redraw();
	}
	void ctlButton::setAsStatic(bool enable) {
		_button_as_static = enable;
	}
	bool ctlButton::isAsStatic() {
		return _button_as_static;
	}
	void ctlButton::_calc_text_rectangle_(bool autoJudgeSize)
	{
		ctlPoint rsize = _getTextSize();
		//计算字体矩形大小
		lockDrawing();
		ctlPoint newSize(getWidth(), getHeight());
		if(autoJudgeSize)
		{
			if((unsigned)_judgeMode & (unsigned)ctlDirection::x) newSize.x = rsize.x + int(std::log(double(rsize.x)) / std::log(1.08));
			if((unsigned)_judgeMode & (unsigned)ctlDirection::y) newSize.y = rsize.y + 10;
			size(newSize);
		}
		unlockDrawing();
		_textRect.x = (getWidth() - rsize.x) / 2;
		_textRect.y = (getHeight() - rsize.y) / 2;
		_textRect.cx = rsize.x, _textRect.cy = rsize.y;
		//调整文字矩形使其不超过控件大小
		_textRect.x = ctlMax(_textRect.x, 3);
		_textRect.cx = ctlMin(_textRect.cx, getWidth() - _textRect.x - 3);
		_textRect.y = ctlMax(_textRect.y, 2);
		_textRect.cy = ctlMin(_textRect.cy, getHeight() - _textRect.y - 2);
		redraw();
	}
	bool ctlButton::setText(const ctlText& lpszText)
	{
		lockDrawing();
		bool result = ctlRoot::setText(lpszText);
		unlockDrawing();
		if(result) _calc_text_rectangle_();
		return result;
	}
	void ctlButton::_flush_font_()
	{
		lockDrawing();
		IFont::_flush_font_();
		unlockDrawing();
		_calc_text_rectangle_();
	}
	void ctlButton::enableSelected(bool b)
	{
		_button_selectedable = b;
		redraw();
	}
	bool ctlButton::isSelectedEnabled()
	{
		return _button_selectedable;
	}
	bool ctlButton::isSelected()
	{
		return _button_selected;
	}
	void ctlButton::setSelected(bool b)
	{
		_button_selected = b;
		redraw();
	}
	void ctlButton::setSelectedStyle(const ctlStyleBase& s)
	{
		_button_style = std::shared_ptr<ctlStyleBase>(s.clone());
		redraw();
	}
	std::shared_ptr<ctlStyleBase> ctlButton::getSelectedStyle()
	{
		return std::shared_ptr<ctlStyleBase>(_button_style->clone());
	}
	ctlStyleBase& ctlButton::getSelectedStyleRef()
	{
		return *_button_style.get();
	}
}